Load scripts and packages. Load the processed data we generated earlier.
library(MoleculeExperiment) # for data structure
library(SpatialExperiment) # for data structure
library(randomForest) # for predictive model
library(scater) # for plotting
library(patchwork) # for plotting
pancreas_colours = readRDS("../analysisOutput/pancreas_colours.Rds")
fit = readRDS("../analysisOutput/sc_cell_type_RF_fit.Rds")
feats = rownames(fit$importance)
me = readRDS("../processedData/xenium_pancreas_me.Rds")
me
## MoleculeExperiment class
##
## molecules slot (1): detected
## - detected:
## samples (1): Xenium_V1_human_Pancreas_FFPE_outs
## -- Xenium_V1_human_Pancreas_FFPE_outs:
## ---- features (541): ABCC11 ACE2 ... VWA5A VWF
## ---- molecules (8073840)
## ---- location range: [3.1,7251.91] x [7.42,2920.68]
##
##
## boundaries slot (2): cell tiles
## - cell:
## samples (1): Xenium_V1_human_Pancreas_FFPE_outs
## -- Xenium_V1_human_Pancreas_FFPE_outs:
## ---- segments (140702): aaaadnje-1 aaacalai-1 ... oimaiaae-1 oimajkkk-1
## - tiles:
## samples (1): Xenium_V1_human_Pancreas_FFPE_outs
## -- Xenium_V1_human_Pancreas_FFPE_outs:
## ---- segments (95060): cell_1 cell_10 ... cell_9998 cell_9999
In this document we take the MoleculeExperiment object, summarise to SpatialExperiment and perform cell type prediction on both assays.
We use the countMolecules function to count the
molecules over the bounaries for either cells or tiles. This generates
SpatialExperiment objects with different numbers of cells/tiles.
We also perform filtering and logcount normalisation.
spe_cells = countMolecules(me, boundariesAssay = "cell")[feats,]
spe_tiles = countMolecules(me, boundariesAssay = "tiles")[feats,]
spe_cells = addPerCellQCMetrics(spe_cells)
spe_tiles = addPerCellQCMetrics(spe_tiles)
spe_cells = logNormCounts(spe_cells[, spe_cells$total >= 5])
spe_tiles = logNormCounts(spe_tiles[, spe_tiles$total >= 5])
spe_cells
## class: SpatialExperiment
## dim: 371 134610
## metadata(0):
## assays(2): counts logcounts
## rownames(371): ABCC11 ACE2 ... VWA5A VWF
## rowData names(0):
## colnames(134610): Xenium_V1_human_Pancreas_FFPE_outs.aaaadnje-1
## Xenium_V1_human_Pancreas_FFPE_outs.aaacalai-1 ...
## Xenium_V1_human_Pancreas_FFPE_outs.oimaiaae-1
## Xenium_V1_human_Pancreas_FFPE_outs.oimajkkk-1
## colData names(8): sample_id cell_id ... total sizeFactor
## reducedDimNames(1): spatial
## mainExpName: NULL
## altExpNames(0):
## spatialCoords names(2) : x_location y_location
## imgData names(1): sample_id
spe_tiles
## class: SpatialExperiment
## dim: 371 82811
## metadata(0):
## assays(2): counts logcounts
## rownames(371): ABCC11 ACE2 ... VWA5A VWF
## rowData names(0):
## colnames(82811): Xenium_V1_human_Pancreas_FFPE_outs.cell_10000
## Xenium_V1_human_Pancreas_FFPE_outs.cell_10001 ...
## Xenium_V1_human_Pancreas_FFPE_outs.cell_9998
## Xenium_V1_human_Pancreas_FFPE_outs.cell_9999
## colData names(8): sample_id cell_id ... total sizeFactor
## reducedDimNames(1): spatial
## mainExpName: NULL
## altExpNames(0):
## spatialCoords names(2) : x_location y_location
## imgData names(1): sample_id
pred_cells = predict(fit, newdata = as.matrix(t(assay(spe_cells, "logcounts"))))
spe_cells$cell_type_pred <- pred_cells
pred_tiles = predict(fit, newdata = as.matrix(t(assay(spe_tiles, "logcounts"))))
spe_tiles$cell_type_pred <- pred_tiles
Generate plots of cells/tiles in space coloured by predicted cell type
g = plotReducedDim(spe_cells, "spatial",
colour_by = "cell_type_pred", point_size = 0.5) +
ggtitle("cells") +
scale_colour_manual(values = pancreas_colours) +
coord_fixed() +
plotReducedDim(spe_tiles, "spatial",
colour_by = "cell_type_pred", point_size = 0.5) +
ggtitle("tiles") +
scale_colour_manual(values = pancreas_colours) +
coord_fixed() +
plot_layout(nrow = 2) +
NULL
g